#ifndef BE_XML_HELPER_H_INCLUDED
#define BE_XML_HELPER_H_INCLUDED

#include "tinyxml.h"
#include <string>

template<size_t COUNT>
class BeXmlHelper
{
	static const size_t MAX_STRLEN = 31;

	enum Type
	{
		eTypeInt,
		eTypeFloat,
		eTypeString
	};

	struct Entry
	{
		char name[MAX_STRLEN+1];
		void* pointer;
		Type type;
	};

	size_t m_count;
	Entry m_entries[COUNT];

public:

	BeXmlHelper() : m_count(0)
	{
	}

	void addInt(const char* const name, int& value)
	{
		addEntry(name, &value, eTypeInt);
	}

	void addFloat(const char* const name, float& value)
	{
		addEntry(name, &value, eTypeFloat);
	}

	void addString(const char* const name, std::string& value)
	{
		addEntry(name, &value, eTypeString);
	}

// 	void parse(const TiXmlElement& element);
// 	template<size_t COUNT>
	void parse( const TiXmlElement& element )
	{
		const TiXmlAttribute* attribute=element.FirstAttribute();
		while(attribute)
		{
			for(size_t i=0;i<m_count;++i)
			{
				Entry& entry=m_entries[i];
				if(std::string(attribute->Name())==entry.name)
				{
					switch(entry.type)
					{
					case eTypeInt:
						*(int*)(entry.pointer)=atoi(attribute->Value());
						break;
					case eTypeFloat:
						*(float*)(entry.pointer)=static_cast<float>(atof(attribute->Value()));
						break;
					case eTypeString:
						*(std::string*)(entry.pointer)=std::string(attribute->Value());
						break;
					default:
						assert(0);
					}
					break;
				}
			}
			attribute=attribute->Next();
		}
	}

private:

// 	void addEntry(const char* const name, void* const pointer, const Type type);
// 	template<size_t COUNT>
	void addEntry( const char* const name, void* const pointer, const Type type )
	{
		assert(m_count<COUNT);
		Entry& entry=m_entries[m_count];
		strncpy(entry.name, name, MAX_STRLEN);
		entry.name[MAX_STRLEN]=0;
		entry.pointer=pointer;
		entry.type=type;
		++m_count;
	}

};

#endif
